import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges} from '@angular/core';
import {BaseService} from '../../../../shared/service/base.service';
import {ApiUrl} from '../../../../config/api-url';
import {PageHelper} from '../../../../utils/page-helper-model';
import {Globals} from "../../../../globals";

@Component({
  selector: 'app-log-login',
  templateUrl: './log-login.component.html',
  styleUrls: ['./log-login.component.less']
})
export class LogLoginComponent implements OnInit {
  isMobile = Globals.isMobile();
  isVisible = false;

  // 集合数据
  listOfData: any;
  // 加载动画
  isOkLoading: boolean;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  // 搜索条件
  keyword: any = "";
  // 开始日期
  startDateTime: Date;
  // 结束日期
  endDateTime: Date;
  // 父子组件传值
  @Output()
  event: EventEmitter<any> = new EventEmitter();

  constructor(public baseService: BaseService) {
  }


  ngOnInit(): void {
    this.findList();
  }

  // 模糊查询
  search() {

    this.findListByKeyword(this.keyword);
  }

  // 获取列表
  findList() {
    this.baseService.findListByKeyword(ApiUrl.LogLoginUrl + '/list', this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.listOfData = data.data.list;
          this.pageHelper.totalItems = data.data.total;
          this.pageHelper.numPages = data.data.pages;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  // 根据keyword条件获取列表
  findListByKeyword(keyword: any) {
    this.pageHelper.keyword = keyword;
    this.pageHelper.currentPage = 1;
    this.findList();
  }


  // 删除
  delete(id: any) {
    this.baseService.layer.modalConfirm('确定要删除该记录吗?', result => {
      if (result) {
        this.baseService.delete(ApiUrl.LogLoginUrl, id).subscribe(
          data => {
            if (data.status == 0) {
              this.baseService.layer.messageSuccess('删除成功！');
              this.loadData();
            } else {
              this.baseService.layer.messageError('删除失败：' + data.msg);
            }
          }
        );
      }
    });
  }


  // 批量删除
  batchDeleteInIds() {

    if (this.baseService.zorro.setOfCheckedId.size == 0){
      this.baseService.layer.messageError('删除集合为空');
      return;
    }

    this.baseService.layer.modalConfirm('确定要批量删除这些记录吗?', result => {
      if (result) {

        // 组装成数组传给后端
        let array = new Array();
        this.baseService.zorro.setOfCheckedId.forEach(item => {
          array.push(item);
        })

        let param = {
          batchDeleteToList:array
        }
        this.baseService.post(ApiUrl.LogLoginUrl + '/batchDeleteInIds', param).subscribe(
          data => {
            if (data.status == 0) {
              this.baseService.layer.messageSuccess('删除成功！');
              this.loadData();
            } else {
              this.baseService.layer.messageError('删除失败：' + data.msg);
            }
          }
        );
      }
    });
  }


  // 清理日志
  clearLog() {
    this.isVisible = true;
  }

  // 批量删除
  batchRemoveLogInIds() {
    this.batchDeleteInIds();
    // this.logSystemComponent.batchDeleteInIds();
  }

  setVisible(body) {
    this.isVisible = body.isVisible;
    // 拿到子组件返回的删除时间段
    if (body.endDateTime && body.endDateTime != null){
      this.startDateTime = body.startDateTime;
      this.endDateTime = body.endDateTime;
      this.batchRemoveLogInStartAndEndDateTime();
    }
  }

  // 根据开始和结束时间来删除
  batchRemoveLogInStartAndEndDateTime(){
    this.deleteInStartAndEndDateTime(this.startDateTime, this.endDateTime);
  }

  // 根据时间段来删除
  deleteInStartAndEndDateTime(startDateTime, endDateTime) {
    let param = {
      startDateTime:startDateTime,
      endDateTime:endDateTime
    }
    console.log(param)
    this.baseService.post(ApiUrl.LogLoginUrl + '/deleteInStartAndEndDateTime', param).subscribe(
      data => {
        if (data.status == 0) {
          this.baseService.layer.messageSuccess('删除成功！');
          this.loadData();
          let body = {'deleteInDateTimeSuccess':true}
          this.event.emit(body)
        } else {
          this.baseService.layer.messageError('删除失败：' + data.msg);
        }
      }
    );
  }

  // 刷新
  loadData() {
    this.pageHelper.currentPage = 1;
    this.findList();
  }


  // 上、下页
  nzPageHelperChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findList();
  }

  // 选中全部
  checkedAll($event: boolean) {
    // 在这里获取当页的所有id

    // 先将集合清空
    this.baseService.zorro.listOfCurrentPageData = this.listOfData;

    // 调用全选方法
    this.baseService.zorro.onAllChecked($event);
  }

  // 选中单个
  checkedItem(id: any, $event: boolean) {
    // 先将集合清空
    this.baseService.zorro.listOfCurrentPageData = this.listOfData;
    this.baseService.zorro.onItemChecked(id, $event)
  }
}
